package com.itheima.jobs;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SendMessageJob {

    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;

    private List<String> telephoneList;
    //定时发送短信
    public void sendMessage() throws Exception {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        System.out.println("今天是:" + f.format(today));
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
        Date tomorrow = c.getTime(); //tomorrow是date类型数据
        String orderDate = f.format(tomorrow);
        List<Integer> memberIdList = orderService.findMemberIdByOrderDate(orderDate);
        for (Integer memberId : memberIdList) {
            String telephone = memberService.findPhoneNumberById(memberId);
            telephoneList.add(telephone);
        }

        //循环手机号发送短信
        for (String telephone : telephoneList) {
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());

        }
    }

}
